package handler

import (
	"context"
	"fmt"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"mxshop_srvs/global"
	"mxshop_srvs/model"
	"mxshop_srvs/proto"
)

// UserServer 编写逻辑代码
type UserServer struct {
	proto.UnimplementedUserServer
}

// GetUserList 获取用户信息
func (s *UserServer) GetUserList(ctx context.Context, pageInfo *proto.PageInfo) (*proto.UserListResponse, error) {
	fmt.Println("UserServer:GetUserList is called!")

	var users []model.User
	result := global.DB.Find(&users)
	fmt.Println(result.RowsAffected)

	userListResponse := new(proto.UserListResponse)

	userListResponse.Total = int32(result.RowsAffected)

	for _, v := range users {
		userInfo := &proto.UserInfoResponse{ // 初始化 userInfo
			Id:       v.ID,
			Password: v.Password,
			Mobile:   v.Mobile,
			NickName: v.NickName,
			Birthday: uint32(v.Birthday.Unix()),
			Gender:   v.Gender,
			Role:     int32(v.Role),
		}
		userListResponse.Data = append(userListResponse.Data, userInfo)
	}
	return userListResponse, nil
}

func (s *UserServer) GetUserMobile(context.Context, *proto.MobileRequest) (*proto.UserInfoResponse, error) {
	return nil, status.Errorf(codes.Unimplemented, "method GetUserMobile not implemented")
}
func (s *UserServer) GetUserID(context.Context, *proto.IDRequest) (*proto.UserInfoResponse, error) {
	return nil, status.Errorf(codes.Unimplemented, "method GetUserID not implemented")
}

//func (s *UserServer) mustEmbedUnimplementedUserServer() {}
